﻿@implements IDisposable
@inject AuthenticationStateProvider StateProvider
@inject ITokenInterceptor Interceptor

@code{
    /// <summary>
    /// 当前会话信息
    /// </summary>
    [CascadingParameter]
    private TokenContext? Context { get; set; }

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        if (Context != null)
        {
            Context.RefreshClientToken += OnRefreshClientToken;
        }
    }

    /// <summary>
    /// OnAfterRenderAsync
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override void OnAfterRender(bool firstRender)
    {
        Context?.UpdateClientTokenCallback();
    }

    /// <summary>
    /// 客户端Token刷新方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    private async void OnRefreshClientToken(object? sender, bool args) => await InvokeAsync(StateHasChanged);

    /// <summary>
    /// Dispose
    /// </summary>
    /// <returns></returns>
    public void Dispose()
    {
        if (Context != null)
        {
            Context.RefreshClientToken -= OnRefreshClientToken;
        }
    }
}